//
//  MapViewController.m
//  BTRIP
//
//  Created by johnbk on 15/11/14.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "MapViewController.h"

@interface MapViewController ()<MKMapViewDelegate, CLLocationManagerDelegate>
/**
 *  定位管理器
 */
@property(nonatomic, retain) CLLocationManager *locationManager;

/**
 *  显示地图
 */
@property(nonatomic, retain) MKMapView *mapView;
/**
 *  当前位置
 */
/**
 *  获取经纬度
 */
@property(nonatomic, assign) CLLocationCoordinate2D curLocationCoordinate;
@property(nonatomic, retain) CLLocation *currentLocation;
@property(nonatomic, retain) Annotation *annotation;
@end

@implementation MapViewController
-(void)dealloc{
	[_address release];
	[_annotation release];
	[_naviView release];
	[_buttonOfReturn release];
	[_locationManager release];
	[_mapView release];
	[_currentLocation release];
	[_annotation release];
	[super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	self.automaticallyAdjustsScrollViewInsets = NO;
	[self createMapView];
	[self createNaviView];
	//[self getCurrentPosition];
	[self getGoodShopPosition];
	
	
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)createMapView{
	self.mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height + 20)];
	self.mapView.mapType = MKMapTypeStandard;
	self.mapView.zoomEnabled = YES;// 可以缩放
	self.mapView.showsUserLocation = YES; //打开定位
	self.mapView.delegate = self;
	[self.view addSubview:self.mapView];
	[self.mapView release];
	
	
}
// 检测用户更新
//- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(nonnull MKUserLocation *)userLocation{
//	NSLog(@"纬度: %f, 经度: %f", userLocation.coordinate.latitude, userLocation.coordinate.longitude);
//	self.currentLocation = userLocation.location;
//	MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.currentLocation.coordinate, 1000, 1000);
//	[self.mapView setRegion:region animated:YES];
//}
// 地图中标记被选中时
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
	if ([view.annotation isKindOfClass:[MKUserLocation class]]) {
		
		[self getReverseGeo];
	}
	else
	{
		NSLog(@"===%@", view.annotation);
		CLGeocoder *revGeo = [[CLGeocoder alloc]init];
		[revGeo reverseGeocodeLocation:self.currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
			if (!error && [placemarks count] > 0) {
				NSDictionary *dict = [[placemarks objectAtIndex:0] addressDictionary];
				NSLog(@"%@", [dict allKeys]);
				
				self.annotation.title = [dict objectForKey:@"Name"];
				
			}
			else
			{
				NSLog(@"ERROR: %@", error);
			}
		}];
	}
}
// 反地理编码
- (void)getReverseGeo{
	CLGeocoder *reverseGeo = [[CLGeocoder alloc]init];
	[reverseGeo reverseGeocodeLocation:self.currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
		if (!error && [placemarks count] > 0) {
			NSDictionary *dict = [[placemarks objectAtIndex:0]addressDictionary];
			self.mapView.userLocation.title = dict[@"City"];
			self.mapView.userLocation.subtitle = dict[@"Name"];
		}
		else{
			NSLog(@"ERROR: %@", error);
		}
	}];
}
// 获取自己的当前位置
//- (void)getCurrentPosition{
//	// 开始勘测自己的位置
//	if (self.locationManager == nil) {
//		self.locationManager = [[CLLocationManager alloc]init];
//		// 初始化定位管理器
//	}
//	if ([CLLocationManager locationServicesEnabled]) {
//		self.locationManager.delegate = self;
//		self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//		self.locationManager.distanceFilter = 10.0f;// 设置方位超过10米时, 调用的代理方法
//		[self.locationManager requestAlwaysAuthorization];
//		[self.locationManager startUpdatingLocation];
//	}
//}
// //获取当前位置信息(只要位置发生改变, 就会调用该方法)
//- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//	self.curLocationCoordinate = locations.lastObject.coordinate;
//	MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(locations.lastObject.coordinate, 1000, 1000);
//	[self.mapView setRegion:region animated:TRUE];
//	self.currentLocation = locations.lastObject;
//}
//- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
//	NSLog(@"error: %@", error);
//}
//// 导航方向发生改变时会调用
//- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
//	NSLog(@"导航方向: %@", newHeading);
//}
//// 进入某个区域后调用
//- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
//	NSLog(@"进入某个区域: %@", region);
//}
//// 走出某个区域
//- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
//	NSLog(@"走出某个区域: %@", region);
//}
- (void)createNaviView{
	self.naviView = [[UIView alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, 84)];
	[self.view addSubview:self.naviView];
	[self.naviView release];
	self.naviView.alpha = 0;
	
	self.buttonOfReturn = [UIButton buttonWithType:UIButtonTypeCustom];
	self.buttonOfReturn.frame = CGRectMake(20, 35, 20, 20);
	[self.buttonOfReturn addTarget:self action:@selector(backToFront:) forControlEvents:UIControlEventTouchUpInside];
	[self.buttonOfReturn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-4"] forState:UIControlStateNormal];
	[self.view addSubview:self.buttonOfReturn];
	
	
	
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
	
	
	if (scrollView.contentOffset.y < 0) {
		
		[self.buttonOfReturn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-4"] forState:UIControlStateNormal];
		self.naviView.alpha = 0;
	}
	else if (scrollView.contentOffset.y > 0 && scrollView.contentOffset.y < 64) {
		self.naviView.alpha = scrollView.contentOffset.y / 64;
		
		[self.buttonOfReturn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-3"] forState:UIControlStateNormal];
		
	}
	else{
		self.naviView.backgroundColor = [UIColor whiteColor];
		self.naviView.alpha = 1;
		
		[self.buttonOfReturn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-3"] forState:UIControlStateNormal];

	}
	
	
}
- (void)backToFront: (UIButton *)button{
	[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
	[self.navigationController.navigationBar setHidden:YES];
}
- (void)getGoodShopPosition{
	
	MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(self.goodShopCoordinate.latitude, self.goodShopCoordinate.longitude), 3000, 3000);
	[self.mapView setRegion:viewRegion];
	Annotation *goodShopAnnotation = [[Annotation alloc]initWithCoordinate:self.goodShopCoordinate andTitle:self.address];
	
	
	[self.mapView addAnnotation:goodShopAnnotation];
	[goodShopAnnotation release];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
